home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c,comp.unix.programmer
- Path: new-news.sprintlink.net!eskimo!scs
- From: scs@eskimo.com (Steve Summit)
- Subject: implicit zero conditionals (was: Q: '\n' character)
- X-Nntp-Posting-Host: eskimo.com
- Message-ID: <DpyM3K.5A6@eskimo.com>
- Followup-To: comp.lang.c
- Sender: news@eskimo.com (News User Id)
- Organization: schmorganization
- References: <4kpd2g$eeb@masala.cc.uh.edu> <1996Apr14.014133.22865@sq.com> <4ku9he$5ee@dawn.mmm.com>
- Date: Tue, 16 Apr 1996 14:40:31 GMT
-
- [Followups redirected to comp.lang.c only, although the topic is
- an old one there.]
-
- In article <4ku9he$5ee@dawn.mmm.com>, cjsonnack@mmm.com (Chris Sonnack) writes:
- > Mark Brader (msb@sq.com) wrote:
- >>>>> if (ptr) *ptr = '\0';
- >>> Ok, I've got a question at this point. Is it really proper to say
- >>> if (ptr)?
- >> Yes. Please reread the comp.lang.c FAQ list.
- >
- > I just finished a Java class...
- > Java has (go figure) eliminated the above ability. The if()
- > (and the loops) MUST HAVE a boolean value there, NOT a zero/non-zero.
- >
- > In Java, you MUST write: if (x != 0) { /** do stuff **/ }
-
- For what it's worth, many of us prefer that style in C as well,
- and write if(p != 0) or if(p != NULL) as a matter of choice.
- The argument is that the controlling expression of an if
- statement is of "conceptual Boolean type", which is what !=
- and the other relational and logical operators return, and which
- a pointer is not. (Programmers of this bent will even write
- if(x != 0) where x is a general-purpose int, reserving the
- condensed form for cases like if(is_vegetable) where the
- variable is_vegetable is itself of conceptual Boolean type.)
-
- But before six people jump on me, I'll save them the trouble
- by saying here that they're not alone, and that plenty of C
- programmers have no problem with if(x), where x has an integral,
- floating-point, or pointer type. The interpretation is no longer
- "is x true?" but "is x nonzero?" (which is, of course, precisely
- the interpretation C is using), or, in the case of pointers,
- perhaps "is x valid?" (although a non-null pointer is necessarily
- valid only if you're careful to initialize all pointers correctly
- and to explicitly set them to null after invalidating them).
-
- Steve Summit
- scs@eskimo.com
-